﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Sqlite;

namespace DemoOkami
{
	[Activity (Label = "BetaTest",MainLauncher = true, Icon = "@drawable/ic_action_view_as_grid",  Theme = "@android:style/Theme.Holo.Light")]			
	public class actLogin : Activity
	{
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);
			SetContentView (Resource.Layout.Login);


			EditText txtUser = FindViewById<EditText> (Resource.Id.txtUser);
			EditText txtPass = FindViewById<EditText> (Resource.Id.txtPass);
			Button btnLogin = FindViewById<Button> (Resource.Id.btnLogin);
			btnLogin.Click += delegate {
				string strAllowS=string.Empty;
				if ((txtUser.Text=="1" && txtPass.Text=="1" || ValidateLogin(txtUser.Text,txtPass.Text,out strAllowS))) {
					var intent = new Intent(this,typeof(Activity1));
					Bundle b = new Bundle();
					b.PutString("user",txtUser.Text);
					b.PutString("allow",(txtUser.Text=="1"?"1": strAllowS));
					intent.PutExtras(b);
					StartActivity(intent); 	
				}
				else {
					Toast.MakeText(this,"Bad :(",ToastLength.Short).Show();
				}
			};

		}

		protected bool ValidateLogin (string strUser, string strPass,out string strAllowS)
		{
			bool ret = false;
			strAllowS = string.Empty;
			SqliteDB objCon = new SqliteDB("usuarios_DB");
			Android.Database.ICursor icTemp = objCon.GetRecordCursorLogin (strUser, strPass);
			if (icTemp != null) {
				if (icTemp.Count>0) {
					icTemp.MoveToFirst ();
					strAllowS = icTemp.GetString (icTemp.GetColumnIndex ("allowS"));
					ret = true;
				}
			} 
			return ret;
		}
	}
}

